package com.example.memo2.ui.home

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.memo2.R
import com.example.memo2.data.model.Memo
import com.example.memo2.data.model.Priority
import com.example.memo2.databinding.ItemMemoBinding
import com.example.memo2.utils.MediaUtils
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class MemoAdapter(
    private val onItemClick: (Memo) -> Unit,
    private val onItemLongClick: (Memo) -> Boolean
) : ListAdapter<Memo, MemoAdapter.MemoViewHolder>(MemosDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MemoViewHolder {
        val binding = ItemMemoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MemoViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MemoViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class MemoViewHolder(private val binding: ItemMemoBinding) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(memo: Memo) {
            binding.apply {
                textTitle.text = memo.title
                textContent.text = memo.content
                textTime.text = formatDate(memo.updatedAt)
                
                // 设置优先级颜色
                setPriorityIndicator(memo.priority)
                
                // 设置点击事件
                root.setOnClickListener { onItemClick(memo) }
                root.setOnLongClickListener { onItemLongClick(memo) }
                
                // 显示提醒图标
                if (memo.reminderTime != null) {
                    iconReminder.visibility = android.view.View.VISIBLE
                } else {
                    iconReminder.visibility = android.view.View.GONE
                }
                
                // 显示多媒体图标
                val hasImages = MediaUtils.parseImagePaths(memo.imagePaths).isNotEmpty()
                val hasAudio = !memo.audioPath.isNullOrBlank()
                
                iconImage.visibility = if (hasImages) android.view.View.VISIBLE else android.view.View.GONE
                iconAudio.visibility = if (hasAudio) android.view.View.VISIBLE else android.view.View.GONE
            }
        }
        
        private fun setPriorityIndicator(priority: Priority) {
            val colorRes = when (priority) {
                Priority.URGENT_IMPORTANT -> R.color.priority_urgent
                Priority.IMPORTANT_NOT_URGENT -> R.color.priority_important
                Priority.NORMAL -> R.color.priority_normal
            }
            val color = ContextCompat.getColor(binding.root.context, colorRes)
            
            // 创建圆形drawable
            val drawable = android.graphics.drawable.GradientDrawable()
            drawable.shape = android.graphics.drawable.GradientDrawable.OVAL
            drawable.setColor(color)
            
            binding.priorityIndicator.background = drawable
        }
        
        private fun formatDate(date: Date): String {
            val now = Date()
            val diff = now.time - date.time
            val dayDiff = diff / (24 * 60 * 60 * 1000)
            
            return when {
                dayDiff == 0L -> SimpleDateFormat("HH:mm", Locale.getDefault()).format(date)
                dayDiff == 1L -> "昨天"
                dayDiff < 7 -> SimpleDateFormat("E", Locale.getDefault()).format(date)
                else -> SimpleDateFormat("MM/dd", Locale.getDefault()).format(date)
            }
        }
    }
}

class MemosDiffCallback : DiffUtil.ItemCallback<Memo>() {
    override fun areItemsTheSame(oldItem: Memo, newItem: Memo): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Memo, newItem: Memo): Boolean {
        return oldItem == newItem
    }
} 